﻿Option Strict On

Imports System
Imports System.Web
Imports System.Web.Services
Imports System.IO
Imports NEvoWeb.Modules.NB_Store.SharedFunctions

Namespace NEvoWeb.Modules.NB_Store.Gateway

    Public MustInherit Class CMCICNotifyProvider
        Implements System.Web.IHttpHandler

#Region "Event Handlers"
        Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Get
                Return True
            End Get
        End Property

        Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

            Try
                UpdateLog("AUTO START")

                Dim objSCtrl As New SettingsController
                Dim objOCtrl As New OrderController
                Dim objPCtrl As New ProductController
                Dim objOInfo As NB_Store_OrdersInfo
                Dim rtnMsg As String = "version=2" & vbLf & "cdr=1" & vbLf

                ' ------------------------------------------------------------------------
                ' In this case the payment provider passes back data via form POST.
                ' Get the data we need.
                Dim textelibre As String = ""
                Dim CMCICStoreOrderID As Integer = 0
                Dim CMCICCartID As String = ""
                Dim CMCICClientLang As String = ""
                If Not context.Request.Form("texte-libre") Is Nothing Then
                    textelibre = context.Request.Form("texte-libre")
                    If textelibre <> "" Then

                        Dim strData As String() = textelibre.Split(";"c)
                        CMCICStoreOrderID = CInt(strData(0))
                        CMCICCartID = strData(1)
                        CMCICClientLang = strData(2)
                        ' ------------------------------------------------------------------------


                        'Log response data so we can tell if the auto response has worked.
                        UpdateLog("AUTO = OrderID:" & CMCICStoreOrderID & " textelibre = " & textelibre)

                        'clear cart parameters, this is not strictly needed but does help tidy things up.
                        Dim objCCtrl As New CartController
                        Dim objCInfo As NB_Store_CartInfo = objCCtrl.GetCart(CMCICCartID)
                        If Not objCInfo Is Nothing Then
                            objCInfo.BankTransID = -1
                            objCInfo.BankHtmlRedirect = ""
                            objCCtrl.UpdateObjCart(objCInfo)
                        End If

                        ' ------------------------------------------------------------------------
                        ' This section updates the order.
                        objOInfo = objOCtrl.GetOrder(CMCICStoreOrderID)

                        If Not objOInfo Is Nothing Then
                            Dim CMCICStatusCode As String = GatewayCMCIC.getStatusCode(objOInfo, context.Request)

                            If CMCICStatusCode = "02" Then
                                rtnMsg = "version=2" & Environment.NewLine & "cdr=1"
                            Else
                                rtnMsg = "version=2" & Environment.NewLine & "cdr=0"
                            End If
                            'Log response data so we can tell if the auto response has worked.
                            UpdateLog("AUTO Status:" & CMCICStatusCode)

                            'check that the order is valid. (Not yet been processed)
                            If Not objOInfo.OrderIsPlaced Then
                                If CMCICStatusCode = "00" Then ' Status return "00" is payment successful
                                    ' check if order has already been processed
                                    If Not objOInfo.OrderIsPlaced Then
                                        'remove qty in trans
                                        objPCtrl.UpdateStockLevel(objOInfo.OrderID)
                                        'set order status to Payed
                                        objOInfo.OrderStatusID = 40
                                        Dim UsrID As Integer = objOInfo.UserID
                                        If UsrID = -1 Then UsrID = 0
                                        objOInfo.OrderNumber = Format(objOInfo.PortalID, "00") & "-" & UsrID.ToString("0000#") & "-" & objOInfo.OrderID.ToString("0000#") & "-" & objOInfo.OrderDate.ToString("yyyyMMdd")
                                        objOInfo.OrderIsPlaced = True
                                        objOInfo.PayType = "CMCIC"
                                        objOCtrl.UpdateObjOrder(objOInfo)

                                        'Send email confirmation
                                        SendEmailToClient(objOInfo.PortalID, GetClientEmail(objOInfo.PortalID, objOInfo), objOInfo.OrderNumber, objOInfo, "paymentOK.email", CMCICClientLang)
                                        SendEmailToManager(objOInfo.PortalID, objOInfo.OrderNumber, objOInfo, "paymentOK.email")
                                    End If
                                Else
                                    'check if order has already been cancelled
                                    If Not objOInfo.OrderStatusID = 30 Then
                                        'remove qty in trans
                                        objPCtrl.RemoveModelQtyTrans(objOInfo.OrderID)
                                        'set order status to cancelled
                                        objOInfo.OrderStatusID = 30
                                        objOCtrl.UpdateObjOrder(objOInfo)
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If

                HttpContext.Current.Response.Clear()
                'HttpContext.Current.Response.ContentType = "text/plain"
                'HttpContext.Current.Response.StatusCode = 200
                HttpContext.Current.Response.Write(rtnMsg)
                'HttpContext.Current.Response.Flush()
                'HttpContext.Current.Response.Close()

                HttpContext.Current.Response.ContentType = "text/plain"
                HttpContext.Current.Response.CacheControl = "no-cache"
                HttpContext.Current.Response.Expires = -1

                'Dim Reponse As String = "version=2" & Environment.NewLine & "cdr=" & "0"

                Try
                    'HttpContext.Current.Response.Write(Reponse)
                    HttpContext.Current.Response.End()
                Catch ex As Exception
                    ' this try/catch to avoid sending error 'ThreadAbortException' (+ email to shop), raised by use of Response.End() 
                End Try

            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex) 'something went wrong, lets log the error...
                SendEmailToAdministrator(0, "CMCIC AutoReturn ERROR", ex.ToString)
            End Try


        End Sub
#End Region

#Region "Private Methods"




#End Region

    End Class

End Namespace
